package Thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.Random;

/**
 * @projectName: Thread-20250806
 * @package: Thread
 * @className: Homework7
 * @author: Tongxinxin
 * @description:实现生产者消费者模型
 * @date: 2025/8/14 1:26
 * @version: 1.0
 */
public class Homework7 {
    public static void main(String[] args) {
        BlockingDeque<Integer> blockingDeque=new LinkedBlockingDeque<>();
        Thread producer=new Thread(()->{
            int n=0;
            while(true){
                try {
                    blockingDeque.put(n);
                    System.out.println("生产元素"+n);
                    n++;
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            });
        Thread customer=new Thread(()->{
                while(true){
                    try {
                        Integer n=blockingDeque.take();
                        System.out.println("消费元素"+n);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
        });
        producer.start();
        customer.start();

    }
}
